<?php
namespace My\Model;

use Utuo\UtuoModel;

class MallGoodsModel extends UtuoModel {
	
	protected $_auto = array(
		array('create_time', 'time', self::MODEL_INSERT, 'function'),
		array('images', 'getImages', self::MODEL_BOTH, 'callback'),
		array('attrs_cfg', 'getAttrsCfg', self::MODEL_BOTH, 'callback'),
		array('content', 'getContent', self::MODEL_BOTH, 'callback'),
		array('reward_percent', 'getRewardPercent', self::MODEL_BOTH, 'callback'),
		array('reward_percent_dealer', 'getRewardPercentDealer', self::MODEL_BOTH, 'callback'),
		array('only_dealer', 'getOnlyDealer', self::MODEL_BOTH, 'callback'),
		array('try', 'getTry', self::MODEL_BOTH, 'callback'),
	);
	
	/**
	 * 仅金牌会员可买
	 */
	protected function getTry() {
		$try = I("try");
		return $try*1;
	}
	
	/**
	 * 仅金牌会员可买
	 */
	protected function getOnlyDealer() {
		$onlyDealer = I("only_dealer");
		return $onlyDealer*1;
	}
	
	/**
	 * 返拥金比例
	 */
	protected function getRewardPercent() {
		$rewardPercent = I("reward_percent");
		$rewards = array();
		$tmp = explode(':', $rewardPercent);
		foreach ( $tmp as $v ) {
			if( $v*1 ) {
				$rewards[] = $v*1;
			} else {
				break;
			}
		}
		return join(':', $rewards);
	}
	
	/**
	 * 返拥金比例
	 */
	protected function getRewardPercentDealer() {
		$rewardPercent = I("reward_percent_dealer");
		$rewards = array();
		$tmp = explode(':', $rewardPercent);
		foreach ( $tmp as $v ) {
			if( $v*1 ) {
				$rewards[] = $v*1;
			} else {
				break;
			}
		}
		return join(':', $rewards);
	}
	
	/**
	 * 上传商品的图片
	 * @return boolean|string|NULL
	 */
	protected function getImages() {
		if(!isset($_REQUEST['images'])) return false;
		$images = I("images", null);
		if ( is_array($images)) {
			return serialize($images);
		}
		return null;
	}
	
	/**
	 * 商品的属性配置
	 * @return boolean|string|NULL
	 */
	protected function getAttrsCfg() {
		if(!isset($_REQUEST['attrs_cfg'])) return false;
		$images = I("attrs_cfg", null);
		if ( is_array($images) ) {
			return serialize($images);
		}
		return null;
	}
	
	/**
	 * 商品内容
	 * @return boolean|Ambigous <mixed, NULL, unknown>
	 */
	protected function getContent() {
		if(!isset($_REQUEST['content'])) return false;
		$content = I("content", "", "htmlspecialchars_decode");
		return $content;
	}
	
	/**
	 * 获得用户将要添加到购物车的商品增加购物车中需要的数据
	 * @param int $id 商品id
	 * @param array $attrs 用户选择的商品属性
	 * array(
	 *   '属性名' => '属性值',
	 * )
	 * @return unknown
	 */
	public function getUserSubmitGoods($id, $attrs) {
		$user = session(session('sessionKey'));
		$goods = $this->find($id);
		if( !$goods ) { $this->error = '商品已不存在'; return false;}
		$goods['attrs_cfg'] = $attributeValues = unserialize($goods['attrs_cfg']);
		if ( $attributeValues ) {
			// 验证商品属性选择
			$selectedAttributes = is_array($attrs) ? $attrs : array(); // 用户选择的商品属性
			foreach ($attributeValues as $k => $v) {
				$v = (array)$v;
				if(!isset($selectedAttributes[$k])) { $this->error = "属性:{$k}未选择."; return false;}
				if(!in_array($selectedAttributes[$k], $v)) { $this->error = "属性值:{$selectedAttributes[$k]}未找到."; return false;}
			}
		}
		$goods['images'] = unserialize($goods['images']);
		$goods['pic'] = $goods['images'] ? $goods['images'][0] : '';
		$goods['selectedAttributes'] = $selectedAttributes;
		$goods['productPrice'] = $this->getGoodsPrice($goods);
		return $goods;
	}
	
	/**
	 * 获得实际运费
	 * @param int $id 商品id
	 * @param int $quantity 购买数量
	 */
	public function getShipping($id, $quantity) {
		$productGoods = $this->find($id);
		return $productGoods['carriage'] * $quantity;
	}
	
	/**
	 * 获得商品单价
	 * @param unknown $goods
	 * @return number
	 */
	private function getGoodsPrice( $goods ) {
		$user = session(session('sessionKey'));
		$price = 0;
		$userRole = new UserRole();
		if ( $goods['try'] ) { // 试用商品
			$price = $goods['try_price'];
		} else { // 非试商品
			if ( $userRole->type($user['type'] )->is(WechatUserModel::USER_TYPE_FRANCHISED_DEALER) ) { // 金牌会员购买
				$goods['dealer_price']*1 and $price = $goods['dealer_price']*1;
			}
			$price or $price = $goods['price']*1;
		}
		return $price;
	}
}